// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.dcel;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
final class SystemUnitLoader implements UnitLoader {
  final static String SYSTEM_PACKAGE_NAME = "com.dustedpixels.daflan";
  
  public void loadUnit(String name, UnitVisitor visitor) {
    if (name.equals(SYSTEM_PACKAGE_NAME + ".And")) {
      loadGate2(visitor, "And");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Or")) {
      loadGate2(visitor, "Or");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Xor")) {
      loadGate2(visitor, "Xor");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Not")) {
      loadGate1(visitor, "Not");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Add")) {
      loadGate2(visitor, "Add");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Sub")) {
      loadGate2(visitor, "Sub");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Inc")) {
      loadGate1(visitor, "Inc");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Dec")) {
      loadGate1(visitor, "Dec");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Shr")) {
      loadGate2(visitor, "Shr");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Shl")) {
      loadGate2(visitor, "Shl");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Shr1")) {
      loadGate1(visitor, "Shr1");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Shl1")) {
      loadGate1(visitor, "Shl1");
    } else if (name.equals(SYSTEM_PACKAGE_NAME + ".Par")) {
      loadGate1(visitor, "Par");
    }
  }
  
  private void loadGate1(UnitVisitor visitor, String name) {
    visitor.visitBegin();
    
    visitor.visitPackageName(SYSTEM_PACKAGE_NAME);
    visitor.visitUnitName(name);
    
    visitor.visitPinsBegin();
    visitor.visitPin("IN");
    visitor.visitPin("OUT");
    visitor.visitPinsEnd();

    visitor.visitEnd();
  }
  
  private void loadGate2(UnitVisitor visitor, String name) {
    visitor.visitBegin();
    
    visitor.visitPackageName(SYSTEM_PACKAGE_NAME);
    visitor.visitUnitName(name);
    
    visitor.visitPinsBegin();
    visitor.visitPin("IN1");
    visitor.visitPin("IN2");
    visitor.visitPin("OUT");
    visitor.visitPinsEnd();

    visitor.visitEnd();
  }
}
